package com.hongdee.crm.coupon.controller;

import com.alibaba.fastjson.JSON;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.DateUtils;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.common.util.StringUtil;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.approval.entity.TCrmCarApprovalFlowDetail;
import com.hongdee.crm.coupon.entity.TCrmCarCoupon;
import com.hongdee.crm.coupon.entity.TCrmCarCouponReissueApply;
import com.hongdee.crm.coupon.service.ITCrmCarCouponService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 名  称:TCrmCarCouponController
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.crm.coupon.controller
 * 作  者: 术洪超
 * 日  期:2017-02-28 11:03
 */

@RestController
@RequestMapping("/crm/coupon")
public class TCrmCarCouponController {

    @Autowired
    private ITCrmCarCouponService itCrmCarCouponService;


    //优惠券管理

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param start 起始记录
     * @param limit 页面大小
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 术洪超
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "") String start,
                    @RequestParam(name = "limit", defaultValue = "") String limit,
                    @RequestParam(name = "activityId", defaultValue = "") String activityId,
                    @RequestParam(name = "status", defaultValue = "") String status,
                    @RequestParam(name = "plateNum", defaultValue = "") String plateNum,
                    @RequestParam(name = "startDate", defaultValue = "") Long startDate,
                    @RequestParam(name = "endDate", defaultValue = "") Long endDate,
                    @RequestParam(name = "deptId", defaultValue = "") String deptId,
                    @RequestParam(name = "createUser", defaultValue = "") String createUser,
                    @RequestParam(name = "approvalStatus", defaultValue = "") String approvalStatus,
                    @RequestParam(name = "vin", defaultValue = "") String vin,
                    @RequestParam(name = "nameOrTel", defaultValue = "") String nameOrTel,
                    @RequestParam(name = "companyId", defaultValue = "") String companyId,
                    @RequestParam(name = "area", defaultValue = "") String area,
                    @RequestParam(name = "brand", defaultValue = "") String brand
    ) throws Exception {

        Map map = itCrmCarCouponService.page(activityId, status, plateNum, startDate, endDate, uid, start, limit, deptId,
                createUser, approvalStatus, vin, nameOrTel, companyId, area, brand);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;

    }

    /**
     * @param data 要保存的优惠券实体
     * @return ResultEntity
     * @throws Exception
     * @desc 保存添加的实体内容
     * @author 术洪超
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "aid", defaultValue = "") String aid,
                             @RequestBody TCrmCarCoupon data) throws Exception {

        //判断必填项
        EntityUtils.valid(data);
        String oid = TokenUtil.getCompanyId(uid);
        data.setCompanyId(oid);
        TCrmCarCoupon coupon = itCrmCarCouponService.save(data, aid, uid);
        ResultEntity res = new ResultEntity();
        res.setData(coupon);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param data
     * @return ResultEntity
     * @throws Exception
     * @desc 打印优惠券（修改状态为1）
     * @author 术洪超
     */
    @RequestMapping("/dayin")
    @ResponseBody
    public ResultEntity dayin(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestBody List<Map> data
    ) throws Exception {
        ResultEntity res = new ResultEntity();
        List list = new ArrayList();
        for (Map map : data) {
            String id = String.valueOf(map.get("id"));
            String youxiaoqi = String.valueOf(map.get("youxiaoqi"));
            itCrmCarCouponService.dayinUpdate(id, "1", uid, youxiaoqi);
        }
        res.setSuccess(true);
        res.setMsg("打印成功");
        return res;
    }


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 删除优惠券（修改状态为-1）
     * @author 术洪超
     */
    @RequestMapping("/del")
    @ResponseBody
    public ResultEntity del(@RequestParam(name = "uid", defaultValue = "") String uid,
                            @RequestParam(name = "token", defaultValue = "") String token,
                            @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        itCrmCarCouponService.update(id, "-1", uid);
        res.setSuccess(true);
        res.setMsg("删除成功");
        return res;
    }

    //消券

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @return ResultEntity
     * @throws Exception
     * @desc 查询详情
     * @author 术洪超
     */
    @RequestMapping("/useInfo")
    @ResponseBody
    public ResultEntity useInfo(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestParam(name = "num", defaultValue = "") String num,
                                @RequestParam(name = "plateNum", defaultValue = "") String plateNum,
                                @RequestParam(name = "activityId", defaultValue = "") String activityId
    ) throws Exception {
        ResultEntity res = new ResultEntity();
        Map map = itCrmCarCouponService.useInfo(num, plateNum, activityId, uid);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param num   优惠券号
     * @return ResultEntity
     * @throws Exception
     * @desc 使用优惠券（修改状态为2）
     * @author 术洪超
     */
    @RequestMapping("/use")
    @ResponseBody
    public ResultEntity use(@RequestParam(name = "uid", defaultValue = "") String uid,
                            @RequestParam(name = "token", defaultValue = "") String token,
                            @RequestParam(name = "num", defaultValue = "") String num,
                            @RequestParam(name = "gongdanId", defaultValue = "") String gongdanId,
                            @RequestParam(name = "useRemark", defaultValue = "") String useRemark,
                            @RequestParam(name = "remainder", defaultValue = "") String remainder,
                            @RequestParam(name = "useMoney", defaultValue = "") String useMoney

    ) throws Exception {
        ResultEntity res = new ResultEntity();
        /**
         * remainder=''时是优惠券
         * remainder！=''时是代金券
         */
        if (remainder == "" || "".equals(remainder) || remainder == "0") {
            itCrmCarCouponService.use(num, "2", useRemark, remainder, useMoney, uid, gongdanId);
        } else {
            itCrmCarCouponService.use(num, "1", useRemark, remainder, useMoney, uid, gongdanId);
        }
        res.setSuccess(true);
        res.setMsg("使用成功");
        return res;
    }


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 查询活动详情
     * @author 术洪超
     */
    @RequestMapping("/activityInfo")
    @ResponseBody
    public ResultEntity activityInfo(@RequestParam(name = "uid", defaultValue = "") String uid,
                                     @RequestParam(name = "token", defaultValue = "") String token,
                                     @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        Map map = itCrmCarCouponService.activityInfo(id, uid);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }


    /**
     * @param uid      登录用户 ID
     * @param token    登录用户 Token
     * @param plateNum 车牌号
     * @return ResultEntity
     * @throws Exception
     * @desc 查询车辆详情，添加优惠券数据
     * @author 术洪超
     */
    @RequestMapping("/carCouponInfo")
    @ResponseBody
    public ResultEntity carCouponInfo(@RequestParam(name = "uid", defaultValue = "") String uid,
                                      @RequestParam(name = "token", defaultValue = "") String token,
                                      @RequestParam(name = "plateNum", defaultValue = "") String plateNum) throws Exception {
        ResultEntity res = new ResultEntity();
        Map map = itCrmCarCouponService.carCouponInfo(plateNum, uid);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param start 起始记录
     * @param limit 页面大小
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 术洪超
     */
    @RequestMapping("/useRecordPage")
    @ResponseBody
    public Map useRecordPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "start", defaultValue = "") String start,
                             @RequestParam(name = "limit", defaultValue = "") String limit,
                             @RequestParam(name = "activityId", defaultValue = "") String activityId,
                             @RequestParam(name = "plateNum", defaultValue = "") String plateNum,
                             @RequestParam(name = "startDate", defaultValue = "") String startDate,
                             @RequestParam(name = "endDate", defaultValue = "") String endDate,
                             @RequestParam(name = "companyId", defaultValue = "") String companyId,
                             @RequestParam(name = "area", defaultValue = "") String area,
                             @RequestParam(name = "brand", defaultValue = "") String brand

    ) throws Exception {

        Map map = itCrmCarCouponService.useRecordPage(activityId, plateNum, startDate, endDate, uid, start, limit, companyId, area, brand);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;

    }


    //补发优惠券

    /**
     * @return List
     * @throws Exception
     * @desc 优惠券补发下拉菜单
     * @author 术洪超
     */
    @RequestMapping("/couponReissueCombo")
    @ResponseBody
    public List<Map> couponReissueCombo(@RequestParam(name = "uid", defaultValue = "") String uid,
                                        @RequestParam(name = "plateNum", defaultValue = "") String plateNum,
                                        @RequestParam(name = "vin", defaultValue = "") String vin
    ) throws Exception {

        return this.itCrmCarCouponService.couponReissueCombo(plateNum, uid,vin);
    }

    /**
     * @param uid      登录用户 ID
     * @param token    登录用户 Token
     * @param plateNum 车牌号
     * @return ResultEntity
     * @throws Exception
     * @desc 查询补发时优惠券详情，添加补发优惠券数据
     * @author 术洪超
     */
    @RequestMapping("/reissueInfo")
    @ResponseBody
    public ResultEntity reissueInfo(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestParam(name = "plateNum", defaultValue = "") String plateNum,
                                    @RequestParam(name = "vin", defaultValue = "") String vin) throws Exception {
        ResultEntity res = new ResultEntity();
        Map map = itCrmCarCouponService.reissueInfo(plateNum, uid,vin);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }


    /**
     * @param data 要保存的补发的优惠券实体
     * @return ResultEntity
     * @throws Exception
     * @desc 保存添加或者修改的实体内容，通过主键判断
     * @author 术洪超
     */
    @RequestMapping("/couponReissueSave")
    @ResponseBody
    public ResultEntity couponReissueSave(@RequestParam(name = "uid", defaultValue = "") String uid,
                                          @RequestParam(name = "token", defaultValue = "") String token,
                                          @RequestBody TCrmCarCouponReissueApply data) throws Exception {

        //判断必填项
        EntityUtils.valid(data);
        String oid = TokenUtil.getCompanyId(uid);
        data.setCompanyId(oid);
        TCrmCarCouponReissueApply reissueApply = itCrmCarCouponService.save(data, uid);
        ResultEntity res = new ResultEntity();
        res.setData(reissueApply);
        res.setSuccess(true);
        res.setMsg("补发成功，等待管理员审核！");
        return res;
    }


    /**
     * @param uid      登录用户 ID
     * @param token    登录用户 Token
     * @param start    起始记录
     * @param limit    页面大小
     * @param applyUid 申请人ID
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 术洪超
     */
    @RequestMapping("/applyPage")
    @ResponseBody
    public Map applyPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                         @RequestParam(name = "token", defaultValue = "") String token,
                         @RequestParam(name = "start", defaultValue = "") String start,
                         @RequestParam(name = "limit", defaultValue = "") String limit,
                         @RequestParam(name = "applyUid", defaultValue = "") String applyUid

    ) throws Exception {

        Map map = itCrmCarCouponService.applyPage(applyUid, uid, start, limit);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;

    }


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    优惠券主键
     * @return ResultEntity
     * @throws Exception
     * @desc 同意补发优惠券（修改状态为1）
     * @author 术洪超
     */
    @RequestMapping("/applyYes")
    @ResponseBody
    public ResultEntity applyYes(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestParam(name = "num", defaultValue = "") String num,
                                 @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        itCrmCarCouponService.applyUpdate(id, num, "1", uid);
        res.setSuccess(true);
        res.setMsg("同意补发");
        return res;
    }

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    优惠券主键
     * @return ResultEntity
     * @throws Exception
     * @desc 拒绝补发优惠券（修改状态为2）
     * @author 术洪超
     */
    @RequestMapping("/applyNo")
    @ResponseBody
    public ResultEntity applyNo(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestParam(name = "num", defaultValue = "") String num,
                                @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        itCrmCarCouponService.applyUpdate(id, num, "2", uid);
        res.setSuccess(true);
        res.setMsg("拒绝补发");
        return res;
    }


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    补发优惠券的ID
     * @return ResultEntity
     * @throws Exception
     * @desc 查询申请补发的优惠券的详情
     * @author 术洪超
     */
    @RequestMapping("/applyInfo")
    @ResponseBody
    public ResultEntity applyInfo(@RequestParam(name = "uid", defaultValue = "") String uid,
                                  @RequestParam(name = "token", defaultValue = "") String token,
                                  @RequestParam(name = "id", defaultValue = "") String id
    ) throws Exception {
        ResultEntity res = new ResultEntity();
        Map map = itCrmCarCouponService.applyInfo(id, uid);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * @param uid      登录用户 ID
     * @param token    登录用户 Token
     * @param start    起始记录
     * @param limit    页面大小
     * @param plateNum 车牌号
     * @param num      优惠券号
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 术洪超
     */
    @RequestMapping("/reissuePage")
    @ResponseBody
    public Map reissuePage(@RequestParam(name = "uid", defaultValue = "") String uid,
                           @RequestParam(name = "token", defaultValue = "") String token,
                           @RequestParam(name = "start", defaultValue = "") String start,
                           @RequestParam(name = "limit", defaultValue = "") String limit,
                           @RequestParam(name = "plateNum", defaultValue = "") String plateNum,
                           @RequestParam(name = "num", defaultValue = "") String num

    ) throws Exception {

        Map map = itCrmCarCouponService.reissuePage(plateNum, num, uid, start, limit);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;

    }

    //激活优惠券

    /**
     * @param uid      登录用户 ID
     * @param token    登录用户 Token
     * @param start    起始记录
     * @param limit    页面大小
     * @param plateNum 车牌号
     * @param num      优惠券号
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 术洪超
     */
    @RequestMapping("/actionPage")
    @ResponseBody
    public Map actionPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                          @RequestParam(name = "token", defaultValue = "") String token,
                          @RequestParam(name = "start", defaultValue = "") String start,
                          @RequestParam(name = "limit", defaultValue = "") String limit,
                          @RequestParam(name = "plateNum", defaultValue = "") String plateNum,
                          @RequestParam(name = "num", defaultValue = "") String num

    ) throws Exception {

        Map map = itCrmCarCouponService.actionPage(plateNum, num, uid, start, limit);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;

    }

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键
     * @return ResultEntity
     * @throws Exception
     * @desc
     * @author 术洪超
     */
    @RequestMapping("/actionInfo")
    @ResponseBody
    public ResultEntity actionInfo(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        Map map = itCrmCarCouponService.actionInfo(id, uid);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 激活优惠券（修改时间为最大值）
     * @author 术洪超
     */
    @RequestMapping("/actionUpdate")
    @ResponseBody
    public ResultEntity actionUpdate(@RequestParam(name = "uid", defaultValue = "") String uid,
                                     @RequestParam(name = "token", defaultValue = "") String token,
                                     @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        itCrmCarCouponService.actionUpdate(id, uid);
        res.setSuccess(true);
        res.setMsg("激活成功");
        return res;
    }


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param data  优惠券主键
     * @return ResultEntity
     * @throws Exception
     * @desc 查询车辆详情，添加优惠券数据
     * @author 术洪超
     */
    @RequestMapping("/couponInfo")
    @ResponseBody
    public ResultEntity couponInfo(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestBody List<Map> data) throws Exception {
        ResultEntity res = new ResultEntity();
        List list = new ArrayList();
        for (Map map : data) {
            String id = String.valueOf(map.get("id"));
            String aid = String.valueOf(map.get("id"));
            Map temp = itCrmCarCouponService.couponInfo(id, uid, aid);
            if (temp.size() > 0) {
                list.add(temp);
            }
        }
        res.setDataList(list);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /****
     * 导出优惠券
     * @param request
     * @param response
     * @param uid
     * @param token
     * @param activityId
     * @param status
     * @param plateNum
     * @param startDate
     * @param endDate
     * @param deptId
     * @param createUser
     * @param approvalStatus
     * @param vin
     * @param nameOrTel
     * @param companyId
     * @param area
     * @param brand
     * @throws Exception
     * @author 张国凯
     */
    //导出优惠券
    @RequestMapping(value = "/excel/exportCoupon")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response,
                            @RequestParam(name = "uid", defaultValue = "") String uid,
                            @RequestParam(name = "token", defaultValue = "") String token,
                            @RequestParam(name = "activityId", defaultValue = "") String activityId,
                            @RequestParam(name = "status", defaultValue = "") String status,
                            @RequestParam(name = "plateNum", defaultValue = "") String plateNum,
                            @RequestParam(name = "startDate", defaultValue = "") Long startDate,
                            @RequestParam(name = "endDate", defaultValue = "") Long endDate,
                            @RequestParam(name = "deptId", defaultValue = "") String deptId,
                            @RequestParam(name = "createUser", defaultValue = "") String createUser,
                            @RequestParam(name = "approvalStatus", defaultValue = "") String approvalStatus,
                            @RequestParam(name = "vin", defaultValue = "") String vin,
                            @RequestParam(name = "nameOrTel", defaultValue = "") String nameOrTel,
                            @RequestParam(name = "companyId", defaultValue = "") String companyId,
                            @RequestParam(name = "area", defaultValue = "") String area,
                            @RequestParam(name = "brand", defaultValue = "") String brand) throws Exception {
        List lists = itCrmCarCouponService.findListCoupon(activityId, status, plateNum, startDate, endDate, uid, deptId,
                createUser, approvalStatus, vin, nameOrTel, companyId, area, brand);

        XSSFWorkbook wb = itCrmCarCouponService.exportCoupon(false,lists, uid);
        response.setContentType("application/vnd.ms-excel");
        String fileName = "优惠券管理";
        response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"),
                "iso8859-1") + DateUtils.getTimeStr("yyyyMMddHHmmss").toString() + ".xls ");
        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();
    }

    /**
     * @Name: 活动导出
     * @Author: Liu XiaoLong
     * @Date: 2017/11/22 16:05
     * @Remark:
     */
    @RequestMapping(value = "/excel/exportActivity")
    public void exportActivity(HttpServletRequest request, HttpServletResponse response,
                            @RequestParam(name = "uid", defaultValue = "") String uid,
                            @RequestParam(name = "token", defaultValue = "") String token,
                            @RequestParam(name = "activityId", defaultValue = "") String activityId,
                            @RequestParam(name = "status", defaultValue = "") String status,
                            @RequestParam(name = "plateNum", defaultValue = "") String plateNum,
                            @RequestParam(name = "startDate", defaultValue = "") Long startDate,
                            @RequestParam(name = "endDate", defaultValue = "") Long endDate,
                            @RequestParam(name = "deptId", defaultValue = "") String deptId,
                            @RequestParam(name = "createUser", defaultValue = "") String createUser,
                            @RequestParam(name = "approvalStatus", defaultValue = "") String approvalStatus,
                            @RequestParam(name = "vin", defaultValue = "") String vin,
                            @RequestParam(name = "nameOrTel", defaultValue = "") String nameOrTel,
                            @RequestParam(name = "companyId", defaultValue = "") String companyId,
                            @RequestParam(name = "area", defaultValue = "") String area,
                            @RequestParam(name = "brand", defaultValue = "") String brand) throws Exception {

        List lists = itCrmCarCouponService.findListActivity(activityId, status, plateNum, startDate, endDate, uid, deptId,
                createUser, approvalStatus, vin, nameOrTel, companyId, area, brand);


        XSSFWorkbook wb = itCrmCarCouponService.exportCoupon(true,lists, uid);
        response.setContentType("application/vnd.ms-excel");
        String fileName = "活动管理";
        response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"),
                "iso8859-1") + DateUtils.getTimeStr("yyyyMMddHHmmss").toString() + ".xls ");
        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();

    }

    //优惠卷详情
    @RequestMapping(value = "/excel/exclActivity")
    public void activityy(HttpServletRequest request, HttpServletResponse response,
                          @RequestParam(name = "uid", defaultValue = "") String uid,
                          @RequestParam(name = "token", defaultValue = "") String token,
                          @RequestParam(name = "activityId", defaultValue = "") String activityId) throws Exception {
        List lists = itCrmCarCouponService.findListCoupono(activityId, uid);
        HSSFWorkbook wb = itCrmCarCouponService.exportRecordy(lists, uid);
        String fileName = "优惠券详情";
        response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"), "iso8859-1") + DateUtils.getTimeStr("yyyyMMddHHmmss").toString() + ".xls ");
        OutputStream outputStream = response.getOutputStream();
        wb.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }


    //导出优惠券使用日志
    @RequestMapping(value = "/excel/exportRecord")
    public void exportExcelRecord(HttpServletRequest request, HttpServletResponse response,
                                  @RequestParam(name = "uid", defaultValue = "") String uid,
                                  @RequestParam(name = "companyId", defaultValue = "") String companyId,
                                  @RequestParam(name = "activityId", defaultValue = "") String activityId,
                                  @RequestParam(name = "plateNum", defaultValue = "") String plateNum,
                                  @RequestParam(name = "startDate", defaultValue = "") String startDate,
                                  @RequestParam(name = "endDate", defaultValue = "") String endDate,
                                  @RequestParam(name = "start", defaultValue = "") String start,
                                  @RequestParam(name = "limit", defaultValue = "") String limit,
                                  @RequestParam(name = "area", defaultValue = "") String area,
                                  @RequestParam(name = "brand", defaultValue = "") String brand
    )
            throws Exception {
        Map pageMap = itCrmCarCouponService.findListRecord(activityId, plateNum, startDate, endDate, uid, start,
                limit, companyId, area, brand);
        List result = pageMap.get("dataList") == null ? new ArrayList() : (List) pageMap.get("dataList");

        HSSFWorkbook wb = itCrmCarCouponService.exportRecord(result, uid);
        response.setContentType("application/vnd.ms-excel");
        String fileName = "优惠券使用日志";
        response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"), "iso8859-1") + DateUtils.getTimeStr("yyyyMMddHHmmss").toString() + ".xls ");
        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();
    }

    /***
     *
     * @param request
     * @param response
     * @param uid
     * @param token
     * @param id activity_id
     * @param type youhuiquanleixin
     * @param timeType shijianleixin
     * @param startDate kaishishijian
     * @param stopDate jieshushijian
     * @throws Exception
     */
    @RequestMapping(value = "/excel/cousponOut")
    public void couponOut(HttpServletRequest request, HttpServletResponse response,
                          @RequestParam(name = "uid", defaultValue = "") String uid,
                          @RequestParam(name = "token", defaultValue = "") String token,
                          @RequestParam(name = "id", defaultValue = "") String id,
                          @RequestParam(name = "type", defaultValue = "") String type,
                          @RequestParam(name = "timeType", defaultValue = "") String timeType,
                          @RequestParam(name = "startDate", defaultValue = "") String startDate,
                          @RequestParam(name = "stopDate", defaultValue = "") String stopDate
    ) throws Exception {
        //查询优惠券信息
        List lists = itCrmCarCouponService.CouponPage(id, type, timeType, startDate, stopDate);
        //优惠券列表转Execl
        HSSFWorkbook wb = itCrmCarCouponService.eCoupon(lists);
        String fileName = "优惠券详情";
        response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"), "iso8859-1") + DateUtils.getTimeStr("yyyyMMddHHmmss").toString() + ".xls ");
        OutputStream outputStream = response.getOutputStream();
        wb.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }

    /**
     * 保险代理 根据活动生成优惠券
     *
     * @param uid
     * @param token
     * @param carId
     * @param nums
     * @return
     */
    @RequestMapping("/createInsuranceActivityCoupon")
    @ResponseBody
    public ResultEntity createInsuranceActivityCoupon(@RequestParam(name = "uid", defaultValue = "") String uid,
                                                      @RequestParam(name = "token", defaultValue = "") String token,
                                                      @RequestParam(name = "carId", defaultValue = "") String carId,
                                                      @RequestParam(name = "activityId", defaultValue = "") String activityId,
                                                      @RequestParam(name = "qId", defaultValue = "") String qId,
                                                      @RequestParam(name = "companyId", defaultValue = "") String companyId,
                                                      @RequestParam(name = "nums", defaultValue = "0") Integer nums) throws Exception {
        ResultEntity res = new ResultEntity();
        //判断数据是否大于0
        if (nums <= 0) {
            throw new Exception("请至少生成一张优惠券信息！！");
        }
        List<Map<String, Object>> lists = this.itCrmCarCouponService.addInsuranceActivityCoupon(activityId, carId, nums, uid, qId,companyId);
        res.setData(lists);
        res.setSuccess(true);
        return res;
    }

    /**
     * 获得车辆对应的报价单优惠券信息
     *
     * @param uid
     * @param token
     * @param carId
     * @param qId
     * @return
     * @throws Exception
     */
    @RequestMapping("/getInsuranceActivityCoupon")
    @ResponseBody
    public ResultEntity createInsuranceActivityCoupon(@RequestParam(name = "uid", defaultValue = "") String uid,
                                                      @RequestParam(name = "token", defaultValue = "") String token,
                                                      @RequestParam(name = "carId", defaultValue = "") String carId,
                                                      @RequestParam(name = "companyId", defaultValue = "") String companyId,
                                                      @RequestParam(name = "qId", defaultValue = "") String qId) throws Exception {
        ResultEntity res = new ResultEntity();
        List<Map<String, Object>> lists = this.itCrmCarCouponService.getInsuranceActivityCoupon(qId, carId,companyId,uid);
        res.setData(lists);
        res.setSuccess(true);
        return res;
    }

    /**
     * 删除保险代理优惠券信息
     *
     * @param uid
     * @param token
     * @param couponId
     * @return
     * @throws Exception
     */
    @RequestMapping("/delInsuranceActivityCoupon")
    @ResponseBody
    public ResultEntity delInsuranceActivityCoupon(@RequestParam(name = "uid", defaultValue = "") String uid,
                                                   @RequestParam(name = "token", defaultValue = "") String token,
                                                   @RequestParam(name = "companyId", defaultValue = "") String companyId,
                                                   @RequestParam(name = "couponId", defaultValue = "") String couponId) throws Exception {
        ResultEntity res = new ResultEntity();
        this.itCrmCarCouponService.delInsuranceActivityCoupon(couponId);
        res.setSuccess(true);
        res.setMsg("成功删除优惠券信息");
        return res;
    }

    /**
     * 更新审核优惠券状态（0未提交1已提交2已审核）
     *
     * @param uid
     * @param token
     * @param data
     * @param status
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateApprovalStatus")
    @ResponseBody
    public ResultEntity updateApprovalStatus(@RequestParam(name = "uid", defaultValue = "") String uid,
                                             @RequestParam(name = "token", defaultValue = "") String token,
                                             //@RequestParam(name = "id", defaultValue = "") String id,
                                             @RequestParam(name = "status", defaultValue = "") String status,
                                             @RequestBody Map data) throws Exception {

        ResultEntity res = new ResultEntity();
        TCrmCarApprovalFlowDetail entity = new TCrmCarApprovalFlowDetail();

        List<String> ids = new ArrayList();
        if (data.get("ids") != "" && data.get("ids") != null) {
            ids = (ArrayList) data.get("ids");
        }
        for (String id : ids) {
            if (data.get("approvalData") != "" && data.get("approvalData") != null) {
                entity = JSON.parseObject(JSON.toJSONString(data.get("approvalData")), TCrmCarApprovalFlowDetail.class);
            }
            this.itCrmCarCouponService.updateApprovalStatus(entity, id, status, uid);
        }
        res.setSuccess(true);
        res.setMsg("更新成功！");
        return res;
    }

    /**
     * 优惠券审批查询
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/approvalPage")
    @ResponseBody
    public Map approvalPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                            @RequestParam(name = "token", defaultValue = "") String token,
                            @RequestParam(name = "start", defaultValue = "0") String start,
                            @RequestParam(name = "limit", defaultValue = "50") String limit,
                            @RequestBody Map data) throws Exception {
        Map map = itCrmCarCouponService.approvalPage(data, start, limit, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 已审批优惠券分页
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/approvaledPage")
    @ResponseBody
    public Map approvaledPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "start", defaultValue = "0") String start,
                              @RequestParam(name = "limit", defaultValue = "50") String limit,
                              @RequestBody Map data) throws Exception {
        Map map = itCrmCarCouponService.approvaledPage(data, start, limit, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }


    /**
     * 保单出单选择优惠券查询
     * @param uid
     * @param token
     * @param activityId
     * @param vin
     * @param companyId
     * @param plateNum
     * @return
     * @throws Exception
     */
    @RequestMapping("/insurance/page")
    @ResponseBody
    public Map insuranceCouponPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "activityId", defaultValue = "") String activityId,
                                   @RequestParam(name = "vin", defaultValue = "") String vin,
                                   @RequestParam(name = "companyId",defaultValue = "") String companyId,
                                   @RequestParam(name = "plateNum", defaultValue = "") String plateNum) throws Exception {
        if("".equals(activityId)){
            throw new Exception("请提供活动信息!!");
        }
        Map<String,Object> map=new HashMap<>();
        map.put("success",true);
        map.put("msg","查询成功");
        List<Map<String,Object>> mapList=itCrmCarCouponService.insuranceCouponPage(uid,companyId,plateNum,vin,activityId);
        map.put("dataList",mapList);
        return map;
    }

    @RequestMapping("/insurance/save")
    @ResponseBody
    public Map insuranceSaveCoupon(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "id", defaultValue = "") String id,
                                   @RequestParam(name = "qId", defaultValue = "") String qId) throws Exception {
        if("".equals(qId)){
            throw new Exception("请提供报价单信息!!");
        }
        Map<String,Object> map=new HashMap<>();
        map.put("success",true);
        map.put("msg","查询成功");
        List<Map<String,Object>> mapList=itCrmCarCouponService.insuranceSaveCoupon(uid,qId,id);
        map.put("dataList",mapList);
        return map;
    }

    @RequestMapping("/insurance/del")
    @ResponseBody
    public Map insuranceDelCoupon(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "id", defaultValue = "") String id,
                                   @RequestParam(name = "qId", defaultValue = "") String qId) throws Exception {
        if("".equals(qId)){
            throw new Exception("请提供报价单信息!!");
        }
        Map<String,Object> map=new HashMap<>();
        map.put("success",true);
        map.put("msg","查询成功");
        List<Map<String,Object>> mapList=itCrmCarCouponService.insuranceDelCoupon(uid,qId,id);
        map.put("dataList",mapList);
        return map;
    }
}
